<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>编辑Blog - 很简单的Blog</title>
<style>
.post {
	width: 600px;
	margin: auto;
	font-size: 14px;
}
.post #title {
	width: 550px;
	border: 2px solid #009821;
}
.post #body {
	width: 600px;
	height: 600px;
	border: 2px solid #009821;
}
</style>
<script src="kindeditor/kindeditor.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>

<div class="post">
	标题：<input id="title" type="text"><hr>
	<textarea id="body"></textarea><hr>
	<button onclick="save();">修改</button>
</div>


</body>
<script>
var blog = {};

$(function () {
	$(document).ready(function () {
		var id = parseInt(window.location.hash.substr(1));
		blog.thisId = id;
		readId(id);
	});
});

KE.show({
	id : 'body',
	afterCreate : function(id) {
		KE.event.ctrl(document, 13, function() {
			KE.sync(id);
		});
		KE.event.ctrl(KE.g[id].iframeDoc, 13, function() {
			KE.sync(id);
		});
	}
});

// 读取
function readId(id) {
	$.getJSON('$read.js', {id: id}, function (d) {
		if (d.error) {
			alert(d.error);
		}
		else if (!d.resoult) {
			alert('指定的文章不存在！');
		}
		else {
			$('#title').val(d.resoult.title);
			KE.html('body', d.resoult.body);
		}
	});
}

// 保存
function save() {
	var id = blog.thisId;
	var title = $('#title').val().trim();
	var body = KE.html('body');
	if (title == '') {
		alert('请填写标题！');
		return;
	}
	$.post('$edit.js', {id: id, title: title, body: body}, function (d) {
		if (d.error) {
			alert(d.error);
		}
		else {
			if (d.affectedRows > 0) {
				alert('修改成功！');
				// window.open('read.html#id=' + d.insertId, '_self');
			}
			else {
				alert('修改失败！\n' + d.message);
			}
		}
	}, 'json');
}
</script>
</html>